package com.todod.base.module.base.service.deviceprotocolconversionrecord;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.todod.base.module.base.controller.admin.deviceprotocolconversionrecord.vo.*;
import com.todod.base.module.base.dal.dataobject.deviceprotocolconversionrecord.DeviceProtocolConversionRecordDO;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.util.object.BeanUtils;

import com.todod.base.module.base.dal.mysql.deviceprotocolconversionrecord.DeviceProtocolConversionRecordMapper;

import static com.todod.base.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.todod.base.module.base.enums.ErrorCodeConstants.*;

/**
 * 仪器协议转换记录管理 Service 实现类
 *
 * @author 平台
 */
@Service
@Validated
public class DeviceProtocolConversionRecordServiceImpl implements DeviceProtocolConversionRecordService {

    @Resource
    private DeviceProtocolConversionRecordMapper deviceProtocolConversionRecordMapper;

    @Override
    public Long createDeviceProtocolConversionRecord(DeviceProtocolConversionRecordSaveReqVO createReqVO) {
        // 插入
        DeviceProtocolConversionRecordDO deviceProtocolConversionRecord = BeanUtils.toBean(createReqVO, DeviceProtocolConversionRecordDO.class);
        deviceProtocolConversionRecordMapper.insert(deviceProtocolConversionRecord);
        // 返回
        return deviceProtocolConversionRecord.getId();
    }

    @Override
    public void updateDeviceProtocolConversionRecord(DeviceProtocolConversionRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceProtocolConversionRecordExists(updateReqVO.getId());
        // 更新
        DeviceProtocolConversionRecordDO updateObj = BeanUtils.toBean(updateReqVO, DeviceProtocolConversionRecordDO.class);
        deviceProtocolConversionRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceProtocolConversionRecord(Long id) {
        // 校验存在
        validateDeviceProtocolConversionRecordExists(id);
        // 删除
        deviceProtocolConversionRecordMapper.deleteById(id);
    }

    private void validateDeviceProtocolConversionRecordExists(Long id) {
        if (deviceProtocolConversionRecordMapper.selectById(id) == null) {
            throw exception(DEVICE_PROTOCOL_CONVERSION_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public DeviceProtocolConversionRecordDO getDeviceProtocolConversionRecord(Long id) {
        return deviceProtocolConversionRecordMapper.selectById(id);
    }

    @Override
    public PageResult<DeviceProtocolConversionRecordDO> getDeviceProtocolConversionRecordPage(DeviceProtocolConversionRecordPageReqVO pageReqVO) {
        return deviceProtocolConversionRecordMapper.selectPage(pageReqVO);
    }

}